<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


/* 关于返回结果的json格式建议如下：
data:执行成功返回的数据
status:简单的表示业务执行的状态，如成功success，登录失败，更新失败等
code:业务执行状态的代码（可不设计）
message:表示详细信息
version:版本号 
*/


class TaskHistory extends MY_Controller
{

    //取得所有任务的年份列表
    public function getyearlist()
    {
        //		echo 'rand:'.$rand;
        //		echo 'workarea'.$workarea;
        $this->load->database();

        //根据用户和团队进行条件选择        
        if ($this->_login_userid) {
            if (isset($_POST['team'])) {
                if ($this->check_is_member($_POST['team'])) {
                    $this->db->where('tid', $_POST['team']);
                } else {
                    $this->db->where('tid', $_POST['team']);
                    $this->db->where('uid', $this->_login_userid);
                }
            } else {
                $this->db->where('uid', $this->_login_userid);
                $this->db->where('tid IS NULL ', NULL, FALSE);
            }
        } else {
            $this->db->where('uid', 5);
        }

        $this->db->select('left(in_date,4) as period,count(*) as task_count', false);
        $this->db->group_by("period", false);
        $this->db->order_by('period', 'desc');
        $this->db->distinct();
        $query = $this->db->get('task');
        if (($num_rows = $query->num_rows()) > 0) {
            $result_array = array(
                'data' => $query->result_array(),
                'status' => 'success',
                'message' => '查询到' . $num_rows . "条记录," . $this->db->last_query()
            );
        } else {
            $result_array = array(
                'status' => 'nofound',
                'message' => '消息' . $this->db->last_query()
            );
        }
        $this->array_to_json($result_array);
    }

    //取得所有任务的月份列表
    public function getmonthlist($year)
    {
        //		echo 'rand:'.$rand;
        //		echo 'workarea'.$workarea;
        $this->load->database();

        //根据用户和团队进行条件选择        
        if ($this->_login_userid) {
            if (isset($_POST['team'])) {
                if ($this->check_is_member($_POST['team'])) {
                    $this->db->where('tid', $_POST['team']);
                } else {
                    $this->db->where('tid', $_POST['team']);
                    $this->db->where('uid', $this->_login_userid);
                }
            } else {
                $this->db->where('uid', $this->_login_userid);
                $this->db->where('tid IS NULL ', NULL, FALSE);
            }
        } else {
            $this->db->where('uid', 5);
        }


        $this->db->select('left(in_date,7) as period,count(*) as task_count', false);
        $this->db->where("DATE_FORMAT(in_date,'%Y')='" . $year . "'", null, false);
        $this->db->group_by("period", false);
        $this->db->order_by('period', 'desc');
        $this->db->distinct();
        $query = $this->db->get('task');

        //		$query = $this->db->query("select distinct left(in_date,7) as month  from  task where uid=".($this->_login_userid?$this->_login_userid:'5')." order by month desc;");
        if (($num_rows = $query->num_rows()) > 0) {
            $result_array = array(
                'data' => $query->result_array(),
                'status' => 'success',
                'message' => '查询到' . $num_rows . "条记录," . $this->db->last_query()
            );
        } else {
            $result_array = array(
                'status' => 'nofound',
                'message' => '消息' . $this->db->last_query()
            );
        }
        $this->array_to_json($result_array);
    }

    //取得某月份的日期列表
    public function getdaylist($month)
    {
        $this->load->database();

        //根据用户和团队进行条件选择        
        if ($this->_login_userid) {
            if (isset($_POST['team'])) {
                if ($this->check_is_member($_POST['team'])) {
                    $this->db->where('tid', $_POST['team']);
                } else {
                    $this->db->where('tid', $_POST['team']);
                    $this->db->where('uid', $this->_login_userid);
                }
            } else {
                $this->db->where('uid', $this->_login_userid);
                $this->db->where('tid IS NULL ', NULL, FALSE);
            }
        } else {
            $this->db->where('uid', 5);
        }

        $this->db->select('left(in_date,10) as period,count(*) as task_count', false);
        $this->db->where("DATE_FORMAT(in_date,'%Y-%m')='" . $month . "'", null, false);
        $this->db->group_by("period", false);
        $this->db->order_by('period', 'desc');
        $this->db->distinct();
        $query = $this->db->get('task');

        /*		if($this->_login_userid){
        $this->db->where('uid', $this->_login_userid);
    }else{
        $this->db->where('uid', 5);
    }
    $query = $this->db->query("select distinct left(in_date,10) as day,count(*) as task_count  from  task where DATE_FORMAT(in_date,'%Y-%m')='".$month."' AND uid=".($this->_login_userid?$this->_login_userid:'5')." group by left(in_date,10) order by day desc;");
*/
        if (($num_rows = $query->num_rows()) > 0) {
            $result_array = array(
                'data' => $query->result_array(),
                'status' => 'success',
                'message' => '查询到' . $num_rows . "条记录," . $this->db->last_query()
            );
        } else {
            $result_array = array(
                'status' => 'nofound',
                'message' => '消息' . $this->db->last_query()
            );
        }
        $this->array_to_json($result_array);
    }

    //取得某月份的所有任务列表,如"2016-01"
    public function getmonthtask($year_month)
    {
        $this->load->database();
        if ($this->_login_userid) {
            $this->db->where('uid', $this->_login_userid);
        } else {
            $this->db->where('uid', 5);
        }
        $this->db->where('DATE_FORMAT(in_date,"%Y-%m")', $year_month);

        $query = $this->db->get('task');
        if (($num_rows = $query->num_rows()) > 0) {
            $result_array = array(
                'data' => $query->result_array(),
                'status' => 'success',
                'message' => '查询到' . $num_rows . "条记录," . $this->db->last_query()
            );
        } else {
            $result_array = array(
                'status' => 'nofound',
                'message' => '消息' . $this->db->last_query()
            );
        }
        $this->array_to_json($result_array);
    }

    //取得某天的所有任务列表，如"2016-01-01"
    public function getdaytask($year_month_day)
    {
        $this->load->database();

        //根据用户和团队进行条件选择        
        if ($this->_login_userid) {
            if (isset($_POST['team'])) {
                if ($this->check_is_member($_POST['team'])) {
                    $this->db->where('tid', $_POST['team']);
                } else {
                    $this->db->where('tid', $_POST['team']);
                    $this->db->where('uid', $this->_login_userid);
                }
            } else {
                $this->db->where('uid', $this->_login_userid);
                $this->db->where('tid IS NULL ', NULL, FALSE);
            }
        } else {
            $this->db->where('uid', 5);
        }

        $this->db->where('DATE_FORMAT(in_date,"%Y-%m-%d")', $year_month_day);
        $this->db->order_by('in_date', 'desc');
        $query = $this->db->get('task');

        if (($num_rows = $query->num_rows()) > 0) {
            $result_array = array(
                'data' => $query->result_array(),
                'status' => 'success',
                'message' => '查询到' . $num_rows . "条记录," . $this->db->last_query()
            );
        } else {
            $result_array = array(
                'status' => 'nofound',
                'message' => '消息' . $this->db->last_query()
            );
        }
        $this->array_to_json($result_array);
    }

}
